import json

from django.shortcuts import render
from django.http import HttpResponse
import uuid
from django.http import JsonResponse
from django.views import View

# Create your views here.

from rest_framework.views import APIView
from rest_framework.response import Response
from auth_app import models
from auth_app.utils.jwt_auth import create_token


class Environment(APIView):

    # 用户角色列表接口
    def get(self, request, *args, **kwargs):
        """
        :param request: 包含三个参数
        query 查询条件
        pagennum 获取页码
        pagesizi 每页条数
        :return:
        """

        if request.GET["query"] == "":
            data_list = models.EnvironmentList.objects.all()
        else:
            data_list = models.EnvironmentList.objects.filter(domain_name__contains=request.GET["query"])

        data_count = data_list.count()  # 获取数据总条数

        page_num = int(request.GET["pagennum"])  # 获取页码
        page_size = int(request.GET["pagesizi"])  # 每页条数
        start_page = (page_num - 1) * page_size  # 开始数据
        end_page = page_num * page_size  # 结束数据

        data_list = list(data_list[start_page:end_page].values())

        get_user = {
            # "data": list(models.Menu.objects.all().values()),
            "data": data_list,
            "total": data_count,
            "meta": {
                "msg": "获取角色列表成功",
                "status": 200
            }}
        return JsonResponse(get_user)


